'接受伤害的组件，用于接受打击组件的信号'

extends Area2D

class_name HurtboxComponent
signal hurt#受到打击后发出信号

@export var health_component: Node

var floating_text_scene = preload("res://场景/ui组件/浮动文本.tscn")


func _ready() -> void:
	area_entered.connect(on_area_entered)
	pass	

#判断碰撞是否为打击组件
func on_area_entered(area: Area2D) -> void:
	if not area is HitboxComponent:
		return
	if health_component == null:
		return
	var hitbox_component = area as HitboxComponent
	health_component.damage(hitbox_component.damage)
	self.hurt.emit()
	var floating_scene = floating_text_scene.instantiate() as Node2D
	self.get_tree().get_first_node_in_group("foreground_layer").add_child(floating_scene)
	floating_scene.global_position = self.global_position + Vector2.UP * 16
	
	var floating_text = "%0.1f"
	if roundf(hitbox_component.damage) == hitbox_component.damage:
		floating_text = "%0.0f"
	floating_scene.start(floating_text % hitbox_component.damage)
	pass
